Code chunks will be hidden in html output. Look at the rmarkdown file for setting-up.
sample datasetStreet Cleaning Function was editted by Chang on 7th February 2020
The following Changes were made:
The Street Dictionary (combined and full) for Manhattan and Brooklyn had several abnormalities. From manually checking, the issues that were identified include:
From the sample dataset, an output of 557,357 rows by 33 columns was derived.
There were 1,478 Enumeration Districts, 1,276 microfilms and 8,557 unique street names within the dataset.
The results from the Street Matching function were as follow:
The results from the Street Matching function were as follow:
Significant improvements were observed in result types 1 and 2 after updating the Street Dictionary
# Distribution of Result Type in Manhattan
plot(table(mn_output$result_type),
type = "h",
col = c("blue", "red", "orange", "purple", "green", "pink"),
lwd = 10,
main = "Updated Result Type for Manhattan",
ylab = "Count",
xlab = "Result Type")
# Get a list of NA and no matches
mn_56_result_list <- mn_output %>% filter(result_type == 5 | result_type == 6) %>% select(township, ED, street_add, best_match, result_type) %>% arrange(ED) %>% dplyr::distinct()
datatable(mn_56_result_list, options = list(autoWidth = TRUE)) %>%
DT::formatStyle(columns = 1:5, fontSize = '70%')
Out of the problematic Street Matches (result type 5 and type 6), there is a trend of some EDs being more problematic than others, i.e. more entries of 5 or 6 within certain Enumeration Districts.
# Problematic EDs for Result Type 5
# Treshold set to 50 (arbitrarily decided)
mn_output %>% filter(result_type == 5) %>%
select(ED) %>% table() %>% sort(decreasing = TRUE) %>% head(19)
## .
## 0815 0009 0836 0043 0676 1272 0451 0818 0291 0049 0142 0363 1643 0766 1000 1202
## 1432 305 226 213 165 153 113 112 101 100 100 100 89 85 78 74
## 0316 1519 1679
## 71 69 69
# Problematic EDs for Result Type 6
# Treshold set to 50 (arbitrarily decided)
mn_output %>% filter(result_type == 6) %>%
select(ED) %>% table() %>% sort(decreasing = TRUE) %>% head(8)
## .
## 1481 1913 0081 0184 0782 0242 1941 0202
## 528 248 113 96 83 75 67 62
# Average result_type by ED
average_result_type_mn <- mn_output %>%
select(ED, result_type) %>%
group_by(ED) %>%
summarise(mean_result_type = mean(result_type))
average_result_type_mn$ED <- as.numeric(average_result_type_mn$ED)
mn_result_type_plot <- ggplot(average_result_type_mn,
aes(x = ED, y = mean_result_type)) +
theme_classic() +
geom_point() +
labs(x = "ED", y = "Average Result Type", title = "Mean Result Type by ED (MN)"
) +
geom_vline(xintercept = 1471, color = "red", linetype = "dotted") +
geom_vline(xintercept = 1652, color = "red", linetype = "dotted")
ggplotly(mn_result_type_plot)
# Standard Deviation of result_type by ED
sd_result_type_mn <- mn_output %>%
select(ED, result_type) %>%
group_by(ED) %>%
summarise(sd_result_type = sd(result_type))
sd_result_type_mn$ED <- as.numeric(sd_result_type_mn$ED)
mn_sd_result_type_plot <- ggplot(sd_result_type_mn,
aes(x = ED, y = sd_result_type)) +
theme_classic() +
geom_point() +
labs(x = "ED", y = "Standard Deviation of Result Type", title = "Standard Deviation of Result Type by ED (MN)"
) +
geom_vline(xintercept = 1394, color = "red", linetype = "dotted") +
geom_vline(xintercept = 1652, color = "red", linetype = "dotted")
ggplotly(mn_sd_result_type_plot)
# Map for Result Type in Manhattan
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(mn_map) + tm_polygons("result_type")
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
Out of the 28% of Perfect Matches (156k entries) 1,717 entries were matched via the Fill Down function 03_Matched_Street_Fill_Down - 1.1%.
Out of 557,357 entries, 159,669 entries were flagged for house number changes (i.e. 0 or 1) - 28.6%.
Previously, 7,350 entries were flagged with “1” meaning the house number was editted by the function. That has now grown to 7,520 (post street cleaning function edits).
This flag refers to: The initial household number might have been “195-7”, “34 TO 36” or “112 114”. There would be a split between the house numbers and hn_1 will be 195 and hn_2 will be 197 as in the first example.
# Problematic Enumeration Districts for House Number Cleaning
mn_output %>% filter(flag_hn_cleaned == 1) %>%
select(ED) %>% table() %>% sort(decreasing = TRUE) %>% head(24)
## .
## 0190 0984 0805 0843 0094 1267 0967 0212 0178 0024 0586 0924 0403 1271 1167 1269
## 634 289 257 245 191 187 177 173 154 143 132 120 102 102 92 85
## 0256 0435 1073 0509 1036 0119 0681 1170
## 82 82 81 79 74 71 63 63
Out of all the house hold number entries, 364,730 entries were filled in via the Fill Down function 05_House_Number_Fill_Down - 65.4%.
That has increased to 368,948 - 66.1% after the street cleaning function was editted.
From the sample dataset, an output of 371,833 rows by 33 columns was derived.
There were 1,106 Enumeration Districts, 1,527 microfilms and 7,076 unique street names within the dataset.
The results from the Street Matching function are as follow:
In contrast to Manhattan, there is a larger proportion of Perfect Matches.
# Distribution of Result type in Brooklyn
plot(table(bk_output$result_type),
type = "h",
col = c("blue", "red", "orange", "purple", "green", "pink"),
lwd = 10,
main = "Result Type for Brooklyn",
ylab = "Count",
xlab = "Result Type")
# Get a list of NA and no matches
bk_56_result_list <- bk_output %>% filter(result_type == 5 | result_type == 6) %>% select(township, ED, street_add, best_match, result_type) %>% arrange(ED) %>% dplyr::distinct()
bk_56_result_list
Out of the problematic Street Matches (result type 5 and type 6), there is a trend of some EDs being more problematic than others, i.e. more entries of 5 or 6 within certain Enumeration Districts.
# Problematic EDs for Result Type 5
# Treshold set to 50 (arbitrarily decided)
bk_output %>% filter(result_type == 5) %>%
select(ED) %>% table() %>% sort(decreasing = TRUE) %>% head(18)
## .
## 0915 1007 0556 1038 0491 0570 0562 0194 0905 1028 0580 1026 0807 1408 0935 0154
## 252 117 104 96 90 79 74 63 63 62 61 61 59 57 55 54
## 0469 0365
## 53 52
# Problematic EDs for Result Type 6
# Treshold set to 50 (arbitrarily decided)
bk_output %>% filter(result_type == 6) %>%
select(ED) %>% table() %>% sort(decreasing = TRUE) %>% head(10)
## .
## 0161 0058 0253 0572 0909 0488 0733 0158 1073 0730
## 100 88 83 78 75 60 56 55 51 46
# Average result_type by ED
average_result_type_bk <- bk_output %>%
select(ED, result_type) %>%
group_by(ED) %>%
summarise(mean_result_type = mean(result_type))
average_result_type_bk$ED <- as.numeric(average_result_type_bk$ED)
bk_result_type_plot <- ggplot(average_result_type_bk,
aes(x = ED, y = mean_result_type)) +
theme_classic() +
geom_point() +
labs(x = "ED", y = "Average Result Type", title = "Mean Result Type by ED (BK)"
) +
geom_vline(xintercept = 1103, color = "red", linetype = "dotted") +
geom_vline(xintercept = 1406, color = "red", linetype = "dotted")
ggplotly(bk_result_type_plot)
# Standard Deviation of result_type by ED
sd_result_type_bk <- bk_output %>%
select(ED, result_type) %>%
group_by(ED) %>%
summarise(sd_result_type = sd(result_type))
sd_result_type_bk$ED <- as.numeric(sd_result_type_bk$ED)
bk_sd_result_type_plot <- ggplot(sd_result_type_bk,
aes(x = ED, y = sd_result_type)) +
theme_classic() +
geom_point() +
labs(x = "ED", y = "Standard Deviation of Result Type", title = "Standard Deviation of Result Type by ED (BK)"
) +
geom_vline(xintercept = 1103, color = "red", linetype = "dotted") +
geom_vline(xintercept = 1406, color = "red", linetype = "dotted")
ggplotly(bk_sd_result_type_plot)
# Map for Result Type in Brooklyn
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(bk_map) + tm_polygons("result_type")
Out of the 76.5% of Perfect Matches (284k entries) 1,005 entries were matched via the Fill Down function 03_Matched_Street_Fill_Down - A much smaller percentage than Manhattan. This suggests that the recording of entries might be more accurate in Brooklynn or that the street directory is more well-developed. Nevertheless, errors are still present.
Out of 371,833 entries, 179,303 entries were flagged for house number changes (i.e. 0 or 1) - 48.2%.
2,563 entries were flagged with “1” meaning the house number was editted by the function. Brooklyn also has several abnormal initial house numbers. E.g. “222 1/2”, “192 3TH”. Otherwise, the issues are similar to Manhattan.
# Problematic Enumeration Districts for House Number Cleaning
bk_output %>% filter(flag_hn_cleaned == 1) %>%
select(ED) %>% table() %>% sort(decreasing = TRUE) %>% head(11)
## .
## 0614 0822 0016 0013 0518 0456 0321 0239 0846 0029 0358
## 335 295 159 149 116 113 108 93 89 86 86
Out of all the house hold number entries, 182,950 entries were filled in via the Fill Down function 05_House_Number_Fill_Down - 49.1%.
- There are missing EDs in the `sample` dataset and missing EDs in the `shapefiles`
- The EDs could be names wrongly
- In the Shapefile, the max ED is 1480 but the max ED in the output runs till 1941
# Missing EDs in Manhattan
# Manhattan ED list
mn_ed_list <- mn_map@data$ED %>% sort()
mn_output_ed <- mn_output$ED %>% unique() %>% as.numeric() %>% sort()
# Which EDs are in the Shapefile but missing from the Manhattan dataset
mn_ed_list[!(mn_output_ed %in% mn_ed_list)]
## [1] 43 47 1431 1519 1742 1743 1745
length(mn_ed_list[!(mn_output_ed %in% mn_ed_list)])
## [1] 7
# Which EDs are in the dataset but missing from the Manhattan shapefile
mn_output_ed[!(mn_ed_list %in% mn_output_ed)]
## [1] 699 1070 1175 1712 1727 1737 1745 1747 1748
length(mn_output_ed[!(mn_ed_list %in% mn_output_ed)])
## [1] 9
# Missing EDs in Brooklyn
# Brooklyn ED list
bk_ed_list <- bk_map@data$ED %>% sort()
# Brooklyn Output EDs
bk_output_ed <- bk_output$ED %>% unique() %>% as.numeric() %>% sort()
# Which EDs are in the Shapefile but missing from the Brooklyn dataset
bk_ed_list[!(bk_output_ed %in% bk_ed_list)]
## [1] 512
length(bk_ed_list[!(bk_output_ed %in% bk_ed_list)])
## [1] 1
# Which EDs are in the dataset but missing from the Brooklyn shapefile
bk_output_ed[!(bk_ed_list %in% bk_output_ed)]
## [1] 761 950 958 963 964 1040 1086
length(bk_output_ed[!(bk_ed_list %in% bk_output_ed)])
## [1] 7
There are 39,311 entries with NA best_match. Their street_add entries are also NA. There are 795 EDs with these problems.
sample data might be corrupted. Or have missing street addressesmn_output %>% filter(is.na(best_match)) %>% sample_n(5)
na_mn_output <- mn_output %>% filter(is.na(best_match)) %>% select(ED) %>% unique()
na_mn_output <- na_mn_output %>% mutate('NA best match' = 1)
datatable(na_mn_output, options = list(autoWidth = TRUE)) %>%
DT::formatStyle(columns = 1:5, fontSize = '70%')
na_mn_map <- merge(mn_map, na_mn_output, 'ED', 'ED')
# Highlight Example
example <- mn_output %>% filter(ED == '0227')
example[41:60,]
# Map for NA Street Address / Best Match in Manhattan
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(na_mn_map) + tm_polygons("NA best match")
# Another problematic best match is "0"
mn_output %>% filter(best_match == 0) %>% sample_n(10)
There are 9,264 entries with NA best_match. Their street_add entries are also NA. There are 427 EDs with these problems.
bk_output %>% filter(is.na(best_match)) %>% sample_n(5)
na_bk_output <- bk_output %>% filter(is.na(best_match)) %>% select(ED) %>% unique()
na_bk_output <- na_bk_output %>% mutate('NA best match' = 1)
datatable(na_bk_output, options = list(autoWidth = TRUE)) %>%
DT::formatStyle(columns = 1:5, fontSize = '70%')
na_bk_map <- merge(bk_map, na_bk_output, 'ED', 'ED')
# Map for NA Street Address / Best Match in Brooklyn
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(na_bk_map) + tm_polygons("NA best match")
# Another problematic best match is "0"
bk_output %>% filter(best_match == 0) %>% sample_n(10)
# Sample of Result Type 1 (Perfect Match)
mn_output %>% filter(result_type == 1) %>% select(ED, street_add, best_match, result_type) %>% head(1)
# Sample of Result Type 2 (Identical Match)
mn_output %>% filter(result_type == 2) %>% select(ED, street_add, best_match, result_type) %>% head(1)
# Sample of Result Type 3 (Singular Mode)
mn_output %>% filter(result_type == 3) %>% select(ED, street_add, best_match, result_type) %>% head(1)
# Sample of Result Type 4 (Multiple Modes)
mn_output %>% filter(result_type == 4) %>% select(ED, street_add, best_match, result_type) %>% head(1)
# Sample of Result Type 1 (NA)
mn_output %>% filter(result_type == 5) %>% select(ED, street_add, best_match, result_type) %>% head(3)
# Sample of Result Type 6 (no match)
mn_output %>% filter(result_type == 6) %>% select(ED, street_add, best_match, result_type) %>% head(4)
List of current issues: * There are “NA” result_type, which are not captured in our 5 and 6s * These entries typically have missing street_add * 38,237 such entries in Manhattan (6.86%) * 8,462 such entries in Brooklyn (2.27%)
result_type 45 and 6 matches
result_type == 6)result_type)
result_type
combine_56_result_list
# Use hunspell to get a list of possible mispelt streets
misspelt <- hunspell_parse(combine_56_result_list$street_add) %>% unlist() %>% hunspell() %>% unlist()
misspelt_list <- misspelt[misspelt != "character(0)"]
misspelt_list %>% head(100)
## [1] "COAVER" "MASHIE" "GRUMVIST" "HASSAN" "POILLION" "GARE"
## [7] "BWAY" "CLUFF" "DAK" "DODANE" "DWAY" "HAULL"
## [13] "MALLERY" "NETHERY" "REEN" "MDW" "STS" "STIVER"
## [19] "STREETOR" "COLER" "RENNICK" "REDERICK" "STOMPSON" "BWAY"
## [25] "GELL" "STEWER" "MAXAM" "RAETH" "DABY" "STR"
## [31] "DEMER" "LUCKTE" "JARVIST" "EIGELBACH" "FORSETH" "ABRIGADOR"
## [37] "JONAR" "DWAIN" "ESSEY" "HESLER" "STR" "CHTYSTIE"
## [43] "CHRYSTEL" "DELANCY" "BROOMES" "OLAMARY" "BRANNAS" "LAWTON"
## [49] "COLUMBAN" "STAUTON" "HOUTON" "SEART" "CASO" "FIANT"
## [55] "ARELENE" "FIANT" "ARLANE" "BEBRA" "PLED" "FITER"
## [61] "OSTER" "DETRZ" "SETREM" "PLESAN" "STRET" "RIRIE"
## [67] "TWNRD" "STR" "LANDHAM" "ELLO" "THTREET" "ROOP"
## [73] "SANDTS" "WACT" "SHIRD" "LEXI" "SEVE" "HUNRED"
## [79] "HEMDRED" "WOSTER" "FW" "FEIST" "SECO" "EIGHTTH"
## [85] "VASSE" "COLUMB" "HUNDRETH" "TENETH" "COURENT" "MARTETTO"
## [91] "NICH" "MCH" "JUMEL" "BRAKSWAY" "DYCKMAN" "DYCKMAN"
## [97] "TRYON" "BWAY" "NIDITO" "ARY"